package siraj;

import ec.EvolutionState;
import ec.Individual;
import ec.Problem;
import ec.simple.SimpleFitness;
import ec.simple.SimpleProblemForm;
import ec.util.Parameter;
import ec.vector.IntegerVectorIndividual;

public class DeploymentProblem extends Problem implements SimpleProblemForm {
	private static final long serialVersionUID = 1L;

	public static final String NUMBEROFNODES = "numberOfNodes";

	public int numberofNodes;

	public void setup(final EvolutionState state, final Parameter base) {
		// very important, remember this
		super.setup(state, base);
		this.numberofNodes = state.parameters.getInt(base.push(NUMBEROFNODES),
				null);
	}

	@Override
	public void describe(Individual ind, EvolutionState state,
			int subpopulation, int threadnum, int log, int verbosity) {
		// TODO Auto-generated method stub
	}

	@Override
	public void evaluate(EvolutionState state, Individual ind,
			int subpopulation, int threadnum) {
		if (ind.evaluated)
			return;

		if (!(ind instanceof IntegerVectorIndividual))
			state.output.fatal("Whoa!  It's not a IntegerVectorIndividual!!!",
					null);

		IntegerVectorIndividual ind2 = (IntegerVectorIndividual) ind;
		float rawfitness = 0;
		for (int x = 0; x < ind2.genome.length; x++) {
			if (ind2.genome[x] < 0)
				ind2.genome[x] = -ind2.genome[x];
			ind2.genome[x] = ind2.genome[x] % this.numberofNodes;
		}

		// TODO
		optDV dep = new optDV();
		dep.init(ind2.genome);
		rawfitness = (float) dep.getFitness();

		if (!(ind2.fitness instanceof SimpleFitness))
			state.output.fatal("Whoa!  It's not a SimpleFitness!!!", null);
		((SimpleFitness) ind2.fitness).setFitness(state, rawfitness, false);

		ind2.evaluated = true;
	}

}
